package com.kayden.springboot.controller;

import com.kayden.springboot.UserInfo;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/resp")
@Controller
//@ResponseBody
//@RestController
//@RestController = @Controller + @ResponseBody
//@Controller : 定义⼀个控制器, Spring 框架启动时加载, 把这个对象交给Spring管理.
//@ResponseBody : 定义返回的数据格式为⾮视图, 返回⼀个 text/html 信息
public class RespController {

    @RequestMapping("/r1")
    public String returnPage() {
        return "/aa/index.html";
    }

    @RequestMapping("r2")
    public String returnData(){
        return "我是前端需要的数据";
    }

    @RequestMapping("/r3")
    @ResponseBody
    public String returnHTML(){
        return "<h1>我是一级标题</h1>";
    }

    @RequestMapping(value = "/r4",produces = "text/plain")
    @ResponseBody
    public  String returnText(){
        return "<h1>我是一级标题</h1>";
    }

    @RequestMapping(value = "/r5")
    @ResponseBody
    public UserInfo returnJSON(){
        UserInfo userInfo = new UserInfo("zhangsan",1,18);
        return userInfo;
    }

    @RequestMapping(value = "/r6")
    @ResponseBody
    public UserInfo setStatus(HttpServletResponse response){
        response.setStatus(404);
        UserInfo userInfo = new UserInfo("zhangsan",1,18);
        return userInfo;
    }

    @ResponseBody
    @RequestMapping(value = "/r7")
    public String setHeader(HttpServletResponse response){
        response.setHeader("myHeader", "myHeader");
        return "设置Header成功";
    }
}
